/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Army.task;

import Army.ArmyEntity;
import Army.BuildingAndUnit;

/**
 *
 * @author Huy
 */
public class TaskDetroy extends Task {
	
	public TaskDetroy(BuildingAndUnit source,BuildingAndUnit target) {
		super(source,target);
		taskName = "Detroy";
	}
	
	@Override
	public boolean execute() {
		if (isDone) {
			return true;
		}
		if (target.isAlive()&& target.getTeam() != source.getTeam()) {
			int dx = Math.abs(target.getPosition().x - source.getPosition().x);
			int dy = Math.abs(target.getPosition().y - source.getPosition().y);
			double d = Math.max(dx, dy);
			source.setTarget(target.getPosition());
			if (d < source.getAttackRange() + target.getSize() / 2) {
				source.attack();
			} else {				
				source.run();
			}
			return false;
		}
		
		source.idle();
		isDone = true;
		return true;
	}
}
